#pragma GCC optimize(2)
#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
#include <bitset>
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll> pii;
const int N=2e5+10;
pii a[N];

void solve()
{
	int n;
	cin>>n;
	ll mnum=1e9+10; 
	for(int i=1;i<=n;i++)
	{
		int m;
		cin>>m;
		a[i].first=a[i].second=1e9+10;
		for(int j=1;j<=m;j++)
		{
			ll num;
			cin>>num;
			mnum=min(num,mnum);
			if(num<a[i].first)
			{
				ll t=a[i].first;
				a[i].first=num;
				a[i].second=t;
			}
			else if(num<a[i].second) a[i].second=num;
		}
	}
	if(n==1)
	{
		cout<<mnum<<endl;
		return ;
	}
	ll sum=0;
	ll ma=1e9+10;
	for(int i=1;i<=n;i++)
	{
		sum+=a[i].second;
		ma=min(a[i].second,ma);
	}
//	cout<<sum<<endl;
	sum=sum-ma+mnum;
	cout<<sum<<endl;
}
int main()
{
	IOS;
	int __t=1;
	cin>>__t;

	while(__t--)
	{
		solve();
	}

	return 0;
}

